<?php

/**
 *  管理中心文章处理程序文件
 */

define('IN_SEXMALL', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . "includes/ckeditor/ckeditor.php");
require_once(ROOT_PATH . 'includes/cls_image.php');

/*初始化数据交换对象 */
$exc   = new exchange(TABLEPRE."article", $db, 'article_id', 'title');
$image = new cls_image();

/* 允许上传的文件类型 */
$allow_file_types = '|GIF|JPG|PNG|BMP|SWF|DOC|XLS|PPT|MID|WAV|ZIP|RAR|PDF|CHM|RM|TXT|';

/*------------------------------------------------------ */
//-- 文章列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
	/* 取得过滤条件 */
	$filter = array();
	$smarty->assign('cat_select',  article_cat_list(0));
	$smarty->assign('ur_here',      $_LANG['03_article_list']);
	$smarty->assign('action_link',  array('text' => $_LANG['article_add'], 'href' => 'article.php?act=add'));
	$smarty->assign('full_page',    1);
	$smarty->assign('filter',       $filter);

	$article_list = get_articleslist();

	$smarty->assign('article_list',    $article_list['arr']);
	$smarty->assign('website',		'http://'.$_SERVER['HTTP_HOST']);
	$smarty->assign('filter',          $article_list['filter']);
	$smarty->assign('record_count',    $article_list['record_count']);
	$smarty->assign('page_count',      $article_list['page_count']);

	$sort_flag  = sort_flag($article_list['filter']);
	$smarty->assign($sort_flag['tag'], $sort_flag['img']);

	assign_query_info();
	$smarty->display('article_list.htm');
}

/*------------------------------------------------------ */
//-- 翻页，排序
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
	check_authz_json('article_manage');

	$article_list = get_articleslist();

	$smarty->assign('article_list',    $article_list['arr']);
	$smarty->assign('filter',          $article_list['filter']);
	$smarty->assign('record_count',    $article_list['record_count']);
	$smarty->assign('page_count',      $article_list['page_count']);
	$smarty->assign('website',		'http://'.$_SERVER['HTTP_HOST']);
	$sort_flag  = sort_flag($article_list['filter']);
	$smarty->assign($sort_flag['tag'], $sort_flag['img']);

	make_json_result($smarty->fetch('article_list.htm'), '',
	array('filter' => $article_list['filter'], 'page_count' => $article_list['page_count']));
}

/*------------------------------------------------------ */
//-- 添加文章
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
	/* 权限判断 */
	admin_priv('article_manage');

	/* 创建 html editor */
	create_html_editor('FCKeditor1');

	/*初始化*/
	$article = array();
	$article['is_open'] = 1;

	/* 取得分类、品牌 */
	$smarty->assign('goods_cat_list', cat_list());
	//$smarty->assign('brand_list',     get_brand_list());

	if (isset($_GET['id']))
	{
		$smarty->assign('cur_id',  $_GET['id']);
	}
	$smarty->assign('article',     $article);
	$smarty->assign('cat_select',  article_cat_list(0));
	$smarty->assign('ur_here',     $_LANG['article_add']);
	$smarty->assign('action_link', array('text' => $_LANG['03_article_list'], 'href' => 'article.php?act=list'));
	$smarty->assign('form_action', 'insert');

	assign_query_info();
	$smarty->display('article_info.htm');
}

/*------------------------------------------------------ */
//-- 添加文章
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'insert')
{
	/* 权限判断 */
	admin_priv('article_manage');

	/*检查是否重复*/
	$is_only = $exc->is_only('title', $_POST['title'],0, " cat_id ='$_POST[article_cat]'");

	if (!$is_only)
	{
		sys_msg(sprintf($_LANG['title_exist'], stripslashes($_POST['title'])), 1);
	}

	/* 取得文件地址 */
	$file_url = '';
	if ((isset($_FILES['file']['error']) && $_FILES['file']['error'] == 0) || (!isset($_FILES['file']['error']) && isset($_FILES['file']['tmp_name']) && $_FILES['file']['tmp_name'] != 'none'))
	{
		// 检查文件格式
		if (!check_file_type($_FILES['file']['tmp_name'], $_FILES['file']['name'], $allow_file_types))
		{
			sys_msg($_LANG['invalid_file']);
		}

		// 复制文件
		$res = upload_article_file($_FILES['file']);
		if ($res != false)
		{
			$file_url = $res;
		}
	}

	if ($file_url == '')
	{
		$file_url = $_POST['file_url'];
	}

	/* 计算文章打开方式 */
	if ($file_url == '')
	{
		$open_type = 0;
	}
	else
	{
		$open_type = $_POST['FCKeditor1'] == '' ? 1 : 2;
	}

	/*插入数据*/
	$add_time = local_time();
	if (empty($_POST['cat_id']))
	{
		$_POST['cat_id'] = 0;
	}

	$startdate = strtotime($_POST['startdate']);
	$enddate = strtotime($_POST['enddate']);
	$sql = "INSERT INTO ".TABLEPRE."article (title, cat_id, article_type, is_open, author, ".
                "author_email, keywords, content, add_time, file_url, open_type, link, description,shortcode,startdate,enddate) ".
            "VALUES ('$_POST[title]', '$_POST[article_cat]', '$_POST[article_type]', '$_POST[is_open]', ".
                "'$_POST[author]', '$_POST[author_email]', '$_POST[keywords]', '$_POST[FCKeditor1]', ".
                "'$add_time', '$file_url', '$open_type', '$_POST[link_url]', '$_POST[description]','','$startdate','$enddate')";
	$db->query($sql);

	/* 处理关联商品 */
	$article_id = $db->insert_id();
	$shortcode = getArtUrlCode($article_id,$_POST[title]);
	if($shortcode) {
		$exc->edit("shortcode='$shortcode'",$article_id);
	}

	$link[0]['text'] = $_LANG['continue_add'];
	$link[0]['href'] = 'article.php?act=add';

	$link[1]['text'] = $_LANG['back_list'];
	$link[1]['href'] = 'article.php?act=list';

	admin_log($_POST['title'],'add','article');

	clear_cache_files(); // 清除相关的缓存文件

	sys_msg($_LANG['articleadd_succeed'],0, $link);
}

/*------------------------------------------------------ */
//-- 编辑
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
	/* 权限判断 */
	admin_priv('article_manage');

	/* 取文章数据 */
	$sql = "SELECT * FROM " .TABLEPRE. "article WHERE article_id='$_REQUEST[id]'";
	$article = $db->GetRow($sql);
	if($article['startdate']) {
		$article['startdate']=date('Y-m-d H:i:s',$article['startdate']);
	}else{
		$article['startdate']='';
	}
	if($article['enddate']) {
		$article['enddate']=date('Y-m-d H:i:s',$article['enddate']);
	}else{
		$article['enddate']='';
	}

	/* 创建 html editor */
	create_html_editor('FCKeditor1',$article['content']);

	/* 取得分类、品牌 */
	$smarty->assign('goods_cat_list', cat_list());
	//$smarty->assign('brand_list', get_brand_list());

	/* 取得关联商品 */
	//$goods_list = get_article_goods($_REQUEST['id']);
	$smarty->assign('goods_list', $goods_list);

	$smarty->assign('article',     $article);
	$smarty->assign('cat_select',  article_cat_list(0, $article['cat_id']));
	$smarty->assign('ur_here',     $_LANG['article_edit']);
	$smarty->assign('action_link', array('text' => $_LANG['03_article_list'], 'href' => 'article.php?act=list&' . list_link_postfix()));
	$smarty->assign('form_action', 'update');

	assign_query_info();
	$smarty->display('article_info.htm');
}

if ($_REQUEST['act'] =='update')
{
	/* 权限判断 */
	admin_priv('article_manage');

	/*检查文章名是否相同*/
	$is_only = $exc->is_only('title', $_POST['title'], $_POST['id'], "cat_id = '$_POST[article_cat]'");

	if (!$is_only)
	{
		sys_msg(sprintf($_LANG['title_exist'], stripslashes($_POST['title'])), 1);
	}


	if (empty($_POST['cat_id']))
	{
		$_POST['cat_id'] = 0;
	}

	/* 取得文件地址 */
	$file_url = '';
	if (empty($_FILES['file']['error']) || (!isset($_FILES['file']['error']) && isset($_FILES['file']['tmp_name']) && $_FILES['file']['tmp_name'] != 'none'))
	{
		// 检查文件格式
		if (!check_file_type($_FILES['file']['tmp_name'], $_FILES['file']['name'], $allow_file_types))
		{
			sys_msg($_LANG['invalid_file']);
		}

		// 复制文件
		$res = upload_article_file($_FILES['file']);
		if ($res != false)
		{
			$file_url = $res;
		}
	}

	if ($file_url == '')
	{
		$file_url = $_POST['file_url'];
	}

	/* 计算文章打开方式 */
	if ($file_url == '')
	{
		$open_type = 0;
	}
	else
	{
		$open_type = $_POST['FCKeditor1'] == '' ? 1 : 2;
	}

	/* 如果 file_url 跟以前不一样，且原来的文件是本地文件，删除原来的文件 */
	$sql = "SELECT file_url FROM " . TABLEPRE . "article WHERE article_id = '$_POST[id]'";
	$old_url = $db->getOne($sql);
	if ($old_url != '' && $old_url != $file_url && strpos($old_url, 'http://') === false && strpos($old_url, 'https://') === false)
	{
		@unlink(ROOT_PATH . $old_url);
	}

	$startdate = strtotime($_POST['startdate']);
	$enddate = strtotime($_POST['enddate']);
	if ($exc->edit("title='$_POST[title]', cat_id='$_POST[article_cat]', article_type='$_POST[article_type]', is_open='$_POST[is_open]', author='$_POST[author]', author_email='$_POST[author_email]', keywords ='$_POST[keywords]', file_url ='$file_url', open_type='$open_type', content='$_POST[FCKeditor1]', link='$_POST[link_url]', description = '$_POST[description]', statistic_code = '".$_POST['statistic_code']."',startdate='$startdate',enddate='$enddate'", $_POST['id']))
	{
		$link[0]['text'] = $_LANG['back_list'];
		$link[0]['href'] = 'article.php?act=list&' . list_link_postfix();

		$note = sprintf($_LANG['articleedit_succeed'], stripslashes($_POST['title']));
		admin_log($_POST['title'], 'edit', 'article');

		clear_cache_files();

		sys_msg($note, 0, $link);
	}
	else
	{
		die($db->error());
	}
}

/*------------------------------------------------------ */
//-- 编辑文章主题
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_title')
{
	check_authz_json('article_manage');

	$id    = intval($_POST['id']);
	$title = json_str_iconv(trim($_POST['val']));

	/* 检查文章标题是否重复 */
	if ($exc->num("title", $title, $id) != 0)
	{
		make_json_error(sprintf($_LANG['title_exist'], $title));
	}
	else
	{
		if ($exc->edit("title = '$title'", $id))
		{
			clear_cache_files();
			admin_log($title, 'edit', 'article');
			make_json_result(stripslashes($title));
		}
		else
		{
			make_json_error($db->error());
		}
	}
}

/*------------------------------------------------------ */
//-- 切换是否显示
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'toggle_show')
{
	check_authz_json('article_manage');

	$id     = intval($_POST['id']);
	$val    = intval($_POST['val']);

	$exc->edit("is_open = '$val'", $id);
	clear_cache_files();

	make_json_result($val);
}

/*------------------------------------------------------ */
//-- 切换文章重要性
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'toggle_type')
{
	check_authz_json('article_manage');

	$id     = intval($_POST['id']);
	$val    = intval($_POST['val']);

	$exc->edit("article_type = '$val'", $id);
	clear_cache_files();

	make_json_result($val);
}



/*------------------------------------------------------ */
//-- 删除文章主题
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
	check_authz_json('article_manage');

	$id = intval($_GET['id']);

	/* 删除原来的文件 */
	$sql = "SELECT file_url FROM " . TABLEPRE . "article WHERE article_id = '$id'";
	$old_url = $db->getOne($sql);
	if ($old_url != '' && strpos($old_url, 'http://') === false && strpos($old_url, 'https://') === false)
	{
		@unlink(ROOT_PATH . $old_url);
	}

	//$name = $exc->get_name($id);
	$sql = 'SELECT title FROM ' . TABLEPRE . "article WHERE article_id = '$id'";
	$name = $db->getOne($sql);

	$sql = 'DELETE FROM ' . TABLEPRE . "article WHERE article_id = '$id'";
	$query = $db->query($sql);
	admin_log($id,'remove', 'article');

	$url = 'article.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
	wap_header("Location: $url\n");
	exit;
}

/*------------------------------------------------------ */
//-- 将商品加入关联
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add_link_goods')
{
	include_once(ROOT_PATH . 'includes/cls_json.php');
	$json = new JSON;

	check_authz_json('article_manage');

	$add_ids = $json->decode($_GET['add_ids']);
	$args = $json->decode($_GET['JSON']);
	$article_id = $args[0];

	if ($article_id == 0)
	{
		$article_id = $db->getOne('SELECT MAX(article_id)+1 AS article_id FROM ' .$ecs->table('article'));
	}

	foreach ($add_ids AS $key => $val)
	{
		$sql = 'INSERT INTO ' . $ecs->table('goods_article') . ' (goods_id, article_id) '.
               "VALUES ('$val', '$article_id')";
		$db->query($sql, 'SILENT') or make_json_error($db->error());
	}

	/* 重新载入 */
	$arr = get_article_goods($article_id);
	$opt = array();

	foreach ($arr AS $key => $val)
	{
		$opt[] = array('value'  => $val['goods_id'],
                        'text'  => $val['goods_name'],
                        'data'  => '');
	}

	make_json_result($opt);
}

/*------------------------------------------------------ */
//-- 将商品删除关联
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'drop_link_goods')
{
	include_once(ROOT_PATH . 'includes/cls_json.php');
	$json = new JSON;

	check_authz_json('article_manage');

	$drop_goods     = $json->decode($_GET['drop_ids']);
	$arguments      = $json->decode($_GET['JSON']);
	$article_id     = $arguments[0];

	if ($article_id == 0)
	{
		$article_id = $db->getOne('SELECT MAX(article_id)+1 AS article_id FROM ' .$ecs->table('article'));
	}

	$sql = "DELETE FROM " . $ecs->table('goods_article').
            " WHERE article_id = '$article_id' AND goods_id " .db_create_in($drop_goods);
	$db->query($sql, 'SILENT') or make_json_error($db->error());

	/* 重新载入 */
	$arr = get_article_goods($article_id);
	$opt = array();

	foreach ($arr AS $key => $val)
	{
		$opt[] = array('value'  => $val['goods_id'],
                        'text'  => $val['goods_name'],
                        'data'  => '');
	}

	make_json_result($opt);
}

/*------------------------------------------------------ */
//-- 搜索商品
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'get_goods_list')
{
	include_once(ROOT_PATH . 'includes/cls_json.php');
	$json = new JSON;

	$filters = $json->decode($_GET['JSON']);

	$arr = get_goods_list($filters);
	$opt = array();

	foreach ($arr AS $key => $val)
	{
		$opt[] = array('value' => $val['goods_id'],
                        'text' => $val['goods_name'],
                        'data' => $val['shop_price']);
	}

	make_json_result($opt);
}
/*------------------------------------------------------ */
//-- 批量操作
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'batch')
{
	/* 批量删除 */
	if (isset($_POST['type']))
	{
		if ($_POST['type'] == 'button_remove')
		{
			admin_priv('article_manage');

			if (!isset($_POST['checkboxes']) || !is_array($_POST['checkboxes']))
			{
				sys_msg($_LANG['no_select_article'], 1);
			}

			/* 删除原来的文件 */
			$sql = "SELECT file_url FROM " . TABLEPRE .
                    "article WHERE article_id " . db_create_in(join(',', $_POST['checkboxes'])) .
                    " AND file_url <> ''";

			$res = $db->query($sql);
			while ($row = $db->fetchRow($res))
			{
				$old_url = $row['file_url'];
				if (strpos($old_url, 'http://') === false && strpos($old_url, 'https://') === false)
				{
					@unlink(ROOT_PATH . $old_url);
				}
			}

			foreach ($_POST['checkboxes'] AS $key => $id)
			{
				$sql = 'DELETE FROM ' . TABLEPRE . "article WHERE article_id = '$id'";
				$query = $db->query($sql);
				if ($query)
				{
					$sql = 'SELECT title FROM ' . TABLEPRE . "article WHERE article_id = '$id'";
					$name = $db->getOne($sql);
					admin_log(addslashes($name),'remove','article');
				}
			}

		}

		/* 批量隐藏 */
		if ($_POST['type'] == 'button_hide')
		{
			check_authz_json('article_manage');
			if (!isset($_POST['checkboxes']) || !is_array($_POST['checkboxes']))
			{
				sys_msg($_LANG['no_select_article'], 1);
			}

			foreach ($_POST['checkboxes'] AS $key => $id)
			{
				$exc->edit("is_open = '0'", $id);
			}
		}

		/* 批量显示 */
		if ($_POST['type'] == 'button_show')
		{
			check_authz_json('article_manage');
			if (!isset($_POST['checkboxes']) || !is_array($_POST['checkboxes']))
			{
				sys_msg($_LANG['no_select_article'], 1);
			}

			foreach ($_POST['checkboxes'] AS $key => $id)
			{
				$exc->edit("is_open = '1'", $id);
			}
		}

		/* 批量移动分类 */
		if ($_POST['type'] == 'move_to')
		{
			check_authz_json('article_manage');
			if (!isset($_POST['checkboxes']) || !is_array($_POST['checkboxes']) )
			{
				sys_msg($_LANG['no_select_article'], 1);
			}

			if(!$_POST['target_cat'])
			{
				sys_msg($_LANG['no_select_act'], 1);
			}

			foreach ($_POST['checkboxes'] AS $key => $id)
			{
				$exc->edit("cat_id = '".$_POST['target_cat']."'", $id);
			}
		}
	}

	/* 清除缓存 */
	clear_cache_files();
	$lnk[] = array('text' => $_LANG['back_list'], 'href' => 'article.php?act=list');
	sys_msg($_LANG['batch_handle_ok'], 0, $lnk);
}

/* 把商品删除关联 */
function drop_link_goods($goods_id, $article_id)
{
	$sql = "DELETE FROM " . $GLOBALS['ecs']->table('goods_article') .
            " WHERE goods_id = '$goods_id' AND article_id = '$article_id' LIMIT 1";
	//$GLOBALS['db']->query($sql);
	create_result(true, '', $goods_id);
}

/* 取得文章关联商品 */
function get_article_goods($article_id)
{
	$list = array();
	$sql  = 'SELECT g.goods_id, g.goods_name'.
            ' FROM ' . $GLOBALS['ecs']->table('goods_article') . ' AS ga'.
            ' LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = ga.goods_id'.
            " WHERE ga.article_id = '$article_id'";
	//$list = $GLOBALS['db']->getAll($sql);

	return $list;
}

/* 获得文章列表 */
function get_articleslist()
{
	$result = get_filter();
	if ($result === false)
	{
		$filter = array();
		$filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
		$filter['keywordshortcode']    = empty($_REQUEST['keywordshortcode']) ? '' : trim($_REQUEST['keywordshortcode']);
		if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
		{
			$filter['keyword'] = json_str_iconv($filter['keyword']);
			$filter['keywordshortcode'] = json_str_iconv($filter['keywordshortcode']);
		}
		$filter['cat_id'] = empty($_REQUEST['cat_id']) ? 0 : intval($_REQUEST['cat_id']);
		$filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'a.article_id' : trim($_REQUEST['sort_by']);
		$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

		$where = '';
		if (!empty($filter['keyword']))
		{
			$where = " AND a.title LIKE '%" . mysql_like_quote($filter['keyword']) . "%'";
		}

		if (!empty($filter['keywordshortcode']))
		{
			$where .= " AND a.shortcode LIKE '%" . mysql_like_quote($filter['keywordshortcode']) . "%'";
		}

		if ($filter['cat_id'])
		{
			$where .= " AND a." . get_article_children($filter['cat_id']);
		}

		/* 文章总数 */
		$sql = 'SELECT COUNT(*) FROM ' .TABLEPRE. 'article AS a '.
               'LEFT JOIN ' .TABLEPRE. 'article_cat AS ac ON ac.cat_id = a.cat_id '.
               'WHERE 1 ' .$where;
		$filter['record_count'] = $GLOBALS['db']->getOne($sql);

		$filter = page_and_size($filter);

		/* 获取文章数据 */
		$sql = 'SELECT a.* , ac.cat_name '.
               'FROM ' .TABLEPRE. 'article AS a '.
               'LEFT JOIN ' .TABLEPRE. 'article_cat AS ac ON ac.cat_id = a.cat_id '.
               'WHERE 1 ' .$where. ' ORDER by '.$filter['sort_by'].' '.$filter['sort_order'];

		$filter['keyword'] = stripslashes($filter['keyword']);
		set_filter($filter, $sql);
	}
	else
	{
		$sql    = $result['sql'];
		$filter = $result['filter'];
	}
	$arr = array();
	$res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

	while ($rows = $GLOBALS['db']->fetchRow($res))
	{
		$rows['date'] = local_date('Y-m-d H:i:s', $rows['add_time']);

		$arr[] = $rows;
	}
	return array('arr' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

/* 上传文件 */
function upload_article_file($upload)
{
	if (!make_dir("../" . DATA_DIR . "/upload/images"))
	{
		/* 创建目录失败 */
		return false;
	}

	$dir = date('Ymd');
	$filename = cls_image::random_filename() . substr($upload['name'], strpos($upload['name'], '.'));
	$dir = ROOT_PATH. DATA_DIR . "/upload/images/" .$dir;
	if (!make_dir($dir))
	{
		return false;
	}
	$path     = $dir.'/'. $filename;

	if (move_upload_file($upload['tmp_name'], $path))
	{
		return DATA_DIR . "/upload/images/" .date('Ymd').'/'. $filename;
	}
	else
	{
		return false;
	}
}

/* 生成短链接码 */
function getArtUrlCode($id,$title) {
	$start = 0;
	$code = md5($id.$title);
	$len = strlen($code);
	$start = rand(0,$len-4);
	$str = substr($code,$start,4);
	if(is_numeric($str)) {
		$str = getArtUrlCode($id,$title);
	}
	if($str) {
		$sql = 'SELECT article_id,title FROM ' .TABLEPRE. 'article WHERE shortcode="'.$str.'"';
		$arr = $GLOBALS['db']->GetRow($sql);
		if($arr) {
			$str = getArtUrlCode($arr['article_id'],$arr['title']);
		}
		return $str;
	}
}

?>